﻿using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;

using TempEmployees.DAL;
using TempEmployees.BLL.EntitiesExtensions;
using TempEmployees.BLL.Database;

namespace TempEmployees.Web
{
    public partial class FormBudgets : ABasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            RegisterToEvents();

            if (!this.IsPostBack)
            {
                SetDropDownLists();
                SetViewLabsGridView();
                SetViewAllRepeater();
            }
        }

        private void RegisterToEvents()
        {
            GridViewDepartmentStatus1.IndexChangedEvent = ViewBudgets_SelectedIndexChanged;
            GridViewAssignmentStatus1.IndexChangedEvent = ViewAssignmentsStatus_SelectedIndexChanged;
            foreach (RepeaterItem item in RepeaterViewAll.Items)
            {
                (item.FindControl("GridViewAssignmentStatus2") as GridViewAssignmentStatus).IndexChangedEvent = ViewAssignmentsStatus_SelectedIndexChanged;
            }
        }

        private void SetViewAllRepeater()
        {
            RepeaterViewAll.DataSource = EDepartment.GetDepartments();
            RepeaterViewAll.DataBind();
        }

        private void SetViewLabsGridView()
        {
            Filters filters = new Filters();
            filters.Add("SemesterID", GetSemesterId());
            GridViewDepartmentStatus1.SetGridView(filters);
        }

        private void SetAssignmentStatusGridView(int labId, GridViewAssignmentStatus gridView)
        {
            List<object> statusList = new List<object>();
            statusList.Add((int)DbConstants.Status.Active);
            statusList.Add((int)DbConstants.Status.Approved);

            Filters filters = new Filters();
            filters.Add("DepartmentID", labId);
            filters.Add("SemesterID", GetSemesterId());
            filters.Add("StatusID", statusList);
            
            gridView.SetGridView(filters);
        }

        private void SetDropDownLists()
        {
            DropDownListSemester.SetDropDownList(
                (list) => DbServices.SetDropDownSemesters(list, true));
            DropDownListSemester.Items.RemoveAt(0);

            if (APageData.Exist<BudgetsStoreData>())
            {
                BudgetsStoreData data = APageData.Get<BudgetsStoreData>();
                DropDownListSemester.SelectedValue = data.SemesterSelectedValue;
                APageData.Remove<BudgetsStoreData>();
            }
        }

        private int GetSemesterId()
        {
            return DropDownListSemester.SelectedValue;
        }

        protected void DropDownBudgetType_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetViewLabsGridView();
            GridViewAssignmentStatus1.Clear();
        }

        protected void ViewBudgets_SelectedIndexChanged(object sender, EventArgs e)
        {
            int labId = GridViewDepartmentStatus1.GetSelectedLabId();
            SetAssignmentStatusGridView(labId, GridViewAssignmentStatus1);
        }

        protected void ViewAssignmentsStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            APageData.Set(new BudgetsStoreData(DropDownListSemester.SelectedValue));

            int assignmentId = GridViewAssignmentStatus.GetSelectedAssignmentId(sender as GridView);
            APageData.Set(new AssignmentDetailsPageData(assignmentId, GUIServices.PAGE_BUDGETS));

            Response.Redirect(GUIServices.PAGE_UPDATE_ASSIGNMENT);
        }

        protected void RepeaterViewAll_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
            Department department = (Department)e.Item.DataItem;
            ((Label)e.Item.FindControl("LabelDepartmentName")).Text = department.Name;
            SetAssignmentStatusGridView(department.ID, ((GridViewAssignmentStatus)e.Item.FindControl("GridViewAssignmentStatus2")));
        }

        protected void ButtonUpdateGridViews_Click(object sender, EventArgs e)
        {
            SetViewLabsGridView();
            GridViewAssignmentStatus1.Clear();
            SetViewAllRepeater();
        }

        protected void ExportToExcelDepartmentsStatus_Click(object sender, EventArgs e) { GridViewDepartmentStatus1.ExportToExcel(); }
        protected void ExportToExcelAssignmentStatus_Click(object sender, EventArgs e) { GridViewAssignmentStatus1.ExportToExcel(); }
    }

    public class BudgetsStoreData : APageData
    {
        public string SemesterSelectedValue { get; private set; }

        public BudgetsStoreData(string semesterSelectedValue)
        {
            SemesterSelectedValue = semesterSelectedValue;
        }
    }
}
